package com.sc.rest.service.impl;

import com.sc.mapper.TbItemCatMapper;
import com.sc.pojo.TbContent;
import com.sc.pojo.TbItemCat;
import com.sc.pojo.TbItemCatExample;
import com.sc.rest.pojo.CatNode;
import com.sc.rest.pojo.ItemCatResult;
import com.sc.rest.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sc on 2017/10/17.
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private TbItemCatMapper itemCatMapper;


    public ItemCatResult getItemCatList() {
        ItemCatResult result = new ItemCatResult();
        result.setData(getItemCat(0L));
        return result;
    }



    private List getItemCat(long parentId){
        //根据parentId查询列表
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        int index = 0;
        List<TbItemCat> list = itemCatMapper.selectByExample(example);
        List result = new ArrayList();
        for (TbItemCat item:list) {
            if(index>=14){
                break;
            }
            if(item.getIsParent()){
            CatNode node  = new CatNode();
            node.setUrl("/products/"+item.getId()+".html");
            if(item.getParentId()==0) {
                node.setName("<a href='/products/" + item.getId() + ".html'>" + item.getName() + "</a>");
               //一级节点不能超过14个
                index++;
            }else {
                node.setName(item.getName());
            }
                node.setItems(getItemCat(item.getId()));
                result.add(node);
            }else {
                String items = "/products/"+item.getId()+".html|"+item.getName();
                result.add(items);
            }

        }
        return result;
    }
}
